home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 235 / Issue 235 - September 2007 - DPCS0907DVD.ISO / Extras / NetObjects Fusion / NOF10.exe / data1.cab / FSI / lib / nof / util / ArrayList.js < prev    next >
Encoding:
Text File  |  2007-04-11  |  7.5 KB  |  265 lines

  1. /****i* SOURCE_FILE/INFO
  2.   *
  3.   * NAME
  4.   *  ArrayList.js
  5.   *
  6.   * USAGE
  7.   *  Part of Netobjects JavaScript Library.
  8.   *
  9.   * COPYRIGHT
  10.   *  Copyright ⌐ 2000-2005 Website Pros, Inc.
  11.   *  All Rights Reserved.
  12.   *
  13.   *  This is an unpublished work protected by Website Pros, Inc.
  14.   *  as a trade secret, and is not to be used or disclosed except as
  15.   *  expressly provided in a written license agreement executed by
  16.   *  you and Website Pros, Inc.
  17.   *
  18.   *      <copyright@websitepros.com>
  19.   *
  20.   * NOTES
  21.   *  JavaScript code.
  22.   *
  23.   *****/
  24. if (!IS.isModuleInitialized("IS.NOF.UTIL.ArrayList"))
  25. {      
  26.   /****h* NOF_JavaScript_Library/NOF.UTIL.ListElement
  27.     *
  28.     * NAME
  29.     *  NOF.UTIL.ListElement
  30.     *
  31.     * DESCRIPTION
  32.     *   Object with a value and a display value for select or list control elements.
  33.     *
  34.     ****/
  35.   function UTIL_ListElement( value, displayValue ) {
  36.     this.__proto__    = UTIL_ListElement.prototype;
  37.     this.value        = value;    
  38.     this.displayValue = displayValue;
  39.   }
  40.   {
  41.     var method        = UTIL_ListElement.prototype;
  42.     /**
  43.       * Equals method
  44.       * @param elem - the element to compare with
  45.       **/
  46.     method.equals    = function ( /*NOF.UTIL.ListElement*/ elem ) {
  47.       if (this.value == elem.value)
  48.         return true;
  49.       else
  50.         return false;
  51.     }
  52.   }    
  53.   
  54.   UTIL.__proto__.ListElement = UTIL_ListElement;
  55.  
  56.   /****h* NOF_JavaScript_Library/NOF.UTIL.ArrayList
  57.     *
  58.     * NAME
  59.     *  NOF.UTIL.ArrayList
  60.     *
  61.     * DESCRIPTION
  62.     *   The javascript array implementation extended with methods of the List interface
  63.     *
  64.     ****/
  65.   
  66.   /**
  67.     * Constructor
  68.     * @param arr - initial array to construct the list from. Can be null.
  69.     **/
  70.   function UTIL_ArrayList( /*Array*/ arr ) {
  71.     if ( arr != null )
  72.       this._list = arr;      
  73.     else
  74.       this._list = new Array();
  75.   }
  76.   {
  77.     var member = UTIL_ArrayList.prototype;
  78.     member._list = null;
  79.     
  80.     var method = UTIL_ArrayList.prototype;
  81.     
  82.     /**
  83.       * Replaces the element at the specified position in this list with the specified element.
  84.       **/
  85.     method.set = function ( /*int*/ index, /*Object*/ elem ){
  86.       this._list[index] = elem;    
  87.     }
  88.     
  89.     /**
  90.       * Returns the element at the specified position in this list.
  91.       **/
  92.     method.get = function ( /*int*/ index ){
  93.       return this._list[index];
  94.     }
  95.     
  96.     /**
  97.       * Inserts the specified element at the specified position in this list.
  98.       * If no index specified, element is added to the end of the list.
  99.       **/
  100.     method.add = function ( /*[int*/ arg1 /*]*/, /*Object*/ arg2 ){
  101.       if (arguments.length == 2)
  102.         this._list[arg1] = arg2;
  103.       else
  104.         this._list[this._list.length] = arg1;      
  105.     }
  106.     
  107.     /**
  108.       * Appends all of the elements in the specified array to the end of 
  109.       * this list, in the order that they are in the array <br>
  110.       * @param col - an array to be inserted into the list
  111.       **/
  112.     method.addAll = function ( /*Array*/ col ){
  113.       var array = col.toArray();
  114.       for (var i=0; i<array.length;i++ )
  115.         this.add( array[i] );
  116.     }
  117.     
  118.     /**
  119.       * Searches for the first occurence of the given argument <br> 
  120.       * Test for equality uses the equals method or '==' if isSimpleObject is true <br>
  121.       * @param elem - an object.
  122.       * @param isSimpleObject - true if object is using '==' and not 'equals' to test equality
  123.       * 
  124.       **/
  125.     method.indexOf = function ( /*Object*/ elem, /*boolean*/ isSimpleObject ) {
  126.       if (elem != null) 
  127.         for( var i=0;i<this._list.length;i++) {
  128.           var isEqual;
  129.           if (arguments.length == 2 && isSimpleObject)
  130.             isEqual = (this._list[i] == elem) ? true : false;
  131.           else
  132.             isEqual = this._list[i].equals (elem);
  133.           if (isEqual)
  134.             return i;
  135.         }
  136.       return -1;
  137.     }
  138.     
  139.     /**
  140.       * Searches for the first occurence of the given argument, testing for equality using the equals method
  141.       * or '==' if isSimpleObject is true. Kept for compatibility with older versions.
  142.       **/
  143.     method.getIndex = method.indexOf;
  144.      
  145.     /**
  146.       * Returns true if this list contains the specified element.
  147.       * <br>
  148.       * Test for equality uses the equals method or '==' if isSimpleObject is true <br>
  149.       * @param elem - an object.
  150.       * @param isSimpleObject - true if object is using '==' and not 'equals' to test equality
  151.       * 
  152.       **/
  153.     method.contains = function ( /*Object*/ elem, /*boolean*/ isSimpleObject ){
  154.       if (this.getIndex(elem, isSimpleObject) != -1)
  155.         return true;      
  156.       return false;
  157.     }
  158.     
  159.     /**
  160.       * Removes the element at the specified position in this list.
  161.       **/
  162.     method.remove = function ( /*int*/ index ){
  163.       for (var j=index; j<this._list.length-1; j++)
  164.         this._list[j] = this._list[j+1];      
  165.       
  166.       this._list.length--;
  167.     }   
  168.     
  169.     /**
  170.       * Swaps two elements in the list
  171.       **/
  172.     method.swap = function ( /*int*/ fromIndex, /*int*/ toIndex){
  173.       var tmp = this._list[fromIndex];
  174.       this._list[fromIndex] = this._list[toIndex];
  175.       this._list[toIndex]   = tmp;
  176.     }  
  177.     
  178.     /**
  179.       * Move the items from left to right (that is from 0 -> length) 
  180.       * with last value getting first (arr[length]->arr[0])
  181.       **/
  182.     method.shiftToRight = function () {
  183.       for (var i=this._list.length-1;i>0; i--) {
  184.         this.swap (i,i-1);
  185.       }
  186.     }
  187.     
  188.     /**
  189.       * Move the items from right to left (that is from length -> 0) 
  190.       * with first value getting last (arr[0]->arr[length])
  191.       **/
  192.     method.shiftToLeft = function () {
  193.       for (var i=1; i<this._list.length; i++) {
  194.         this.swap (i-1,i);
  195.       }
  196.     }
  197.     
  198.     /**
  199.       * Move the item one element to the right or the left (depending on direction +/-)
  200.       **/
  201.     method.shiftItem = function (/*int*/ index, /*int*/ direction) {
  202.       var newIndex = index + direction;
  203.       if (newIndex < 0) {
  204.         this.shiftToLeft();
  205.       }
  206.       else if (newIndex > this._list.length-1) {
  207.         this.shiftToRight();
  208.       }
  209.       else {
  210.         this.swap (newIndex, index);
  211.       }
  212.     }
  213.     
  214.     /**
  215.       * Move the item 'n' elements to the right or the left (depending if n is +/-)
  216.       **/
  217.     method.shiftItemWithN = function (/*int*/ index, /*int*/ n) {
  218.       var direction = (n > 0) ? +1 : -1;
  219.       for (var i=0; i<Math.abs(n);i++) {
  220.         var newIndex = index + i*direction;
  221.         this.shiftItem (newIndex,direction);
  222.       }
  223.     }    
  224.     
  225.     /**
  226.       * Tests if this list has no elements.
  227.       **/
  228.     method.isEmpty = function (){
  229.       if (this._list.length == 0 )
  230.         return true;
  231.       else
  232.         return false;
  233.     }
  234.     
  235.     /**
  236.       * Removes all of the elements from this list.
  237.       **/
  238.     method.clear = function (){
  239.       this._list = new Array();
  240.     }
  241.  
  242.     /**
  243.       * Returns the number of elements in this list.
  244.       **/
  245.     method.size = function (){
  246.       return this._list.length;
  247.     }
  248.     
  249.     /**
  250.       * Sorts the list using the JavaScript Array.sort function
  251.       **/
  252.     method.sort = function () {
  253.       this._list.sort();
  254.     }
  255.     
  256.     /**
  257.       * Returns an array containing all of the elements in this list in the correct order.
  258.       **/
  259.     method.toArray = function (){
  260.       return this._list;
  261.     }
  262.   }
  263.   
  264.   UTIL.__proto__.ArrayList = UTIL_ArrayList;
  265. }